<?php


/**
 * Notes:
 * PROJECT_NAME : ERP v1.8 重构erp
 * User: 夏鱼儿<397072174@qq.com>
 * Date: 2024/6/6
 * Time: 16:14
 */

namespace app\command\company;

use app\admin\model\Company;
use app\company\model\DeliverLine;
use app\tms\model\StoreOrder;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use app\company\model\PickingLine as PickingLineModel;
use app\company\model\DeliverTask;

class PickingLine extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('picking:line')->setDescription('路线分拣单')
             ->addArgument('date', Option::VALUE_REQUIRED, '日期', '');
    }


    protected function execute(Input $input, Output $output)
    {
        $date = $input->getArgument('date');
        if (!$date) $date = date('Ymd');
        $date  = date('Y-m-d', strtotime($date));
        $model = new DeliverLine();
        try {
            $arr = $model->column('id deliver_line_id,name,company_id');
            foreach ($arr as $k => $v) {
                $arr[$k]['pick_time'] = strtotime($date);
                $arr[$k]['name']      = $v['name'] . $date . "分拣单";
                //没有任务则删除
                if (!DeliverTask::where('deliver_line_id', $v['deliver_line_id'])->whereDay('create_time', 'yesterday')->count()) {
                    unset($arr[$k]);
                }
            }

            if ($arr) (new PickingLineModel())->saveAll($arr);
            $output->writeln($date . '路线分拣单生成成功');
        } catch (\Exception $e) {
            $output->writeln('路线分拣单异常：' . $e->getMessage());
        }
    }
}